﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpPromotionEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string strSql = @"with t as (select Id,Pid,cast(Text as nvarchar(500)) Text,OrderNo,1 lvl,cast(Id as nvarchar(50)) orderStr from Depts where Pid is null
union all
select d.Id,d.Pid,cast(REPLICATE('--',t.lvl)+d.Text as nvarchar(500)),
d.OrderNo,t.lvl+1,cast(t.orderStr+cast(d.Id as nvarchar(50)) as nvarchar(50))
from Depts d,t where d.Pid=t.id
)
  select Id,Text from t order by orderStr;";
                var depts = ctx.ExecuteQuery<DeptDTO>(strSql);
                Dept.DataSource = depts;
                Dept.DataValueField = "Id";
                Dept.DataTextField = "Text";
                Dept.DataBind();

                var positions = from r in ctx.Positions select new { r.Id, r.Name };
                Position.DataSource = positions;
                Position.DataValueField = "Id";
                Position.DataTextField = "Name";
                Position.DataBind();

                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id) && id != "undefined")
                {
                    var v = ctx.EmpPromotions.Single(r => r.Id == int.Parse(id));
                    if(v.StartDate!=null)
                    {
                        StartDate.Value =((DateTime)v.StartDate).ToString("yyyy-MM-dd");
                    }
                    EndDate.Value = v.EndDate.ToString("yyyy-MM-dd");
                    Duty.Value = v.Duty;
                    Dept.Value = v.DeptId.ToString();
                    Position.Value = v.PosId.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            EmpPromotion empPromotion;
            if (!string.IsNullOrEmpty(id) && id != "undefined")
            {
                empPromotion = ctx.EmpPromotions.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                empPromotion = new EmpPromotion();
                empPromotion.EmpId = int.Parse(Request.QueryString["empId"]);
                ctx.EmpPromotions.InsertOnSubmit(empPromotion);
            }
            if (!string.IsNullOrWhiteSpace(StartDate.Value))
            {
                empPromotion.StartDate = DateTime.Parse(StartDate.Value);
            }
            empPromotion.EndDate = DateTime.Parse(EndDate.Value);
            empPromotion.Duty =Duty.Value;
            empPromotion.DeptId = int.Parse(Dept.Value);
            empPromotion.PosId = int.Parse(Position.Value);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='" + empPromotion.Id + "';window.close();</script>");
        }
    }
}